//package org.jjche.file.conf;
//
//import cn.hutool.core.collection.CollUtil;
//import cn.hutool.core.util.ArrayUtil;
//import cn.hutool.system.oshi.OshiUtil;
//import org.dromara.x.file.storage.spring.SpringFileStorageProperties;
//import org.jjche.common.util.FileUtil;
//import org.jjche.common.util.StrUtil;
//import org.jjche.core.constant.FileConstant;
//import org.jjche.core.constant.OSConstant;
//import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
//import org.springframework.context.annotation.Configuration;
//import org.springframework.web.servlet.config.annotation.EnableWebMvc;
//import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
//import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
//
//import javax.annotation.Resource;
//import java.util.List;
//
///**
// * <p>
// * 配置本地文件访问路径
// * </p>
// *
// * @author miaoyj
// * @since 2024-07-11
// */
//@Configuration
//@EnableWebMvc
//@ConditionalOnProperty
//public class FileConfigureAdapter implements WebMvcConfigurer {
//
//    @Resource
//    private SpringFileStorageProperties fileStorageProperties;
//
//    /**
//     * {@inheritDoc}
//     */
//    @Override
//    public void addResourceHandlers(ResourceHandlerRegistry registry) {
//        List<? extends SpringFileStorageProperties.SpringLocalPlusConfig> localPlusConfigs = fileStorageProperties.getLocalPlus();
//        SpringFileStorageProperties.SpringLocalPlusConfig localPlusConfig = CollUtil.getFirst(localPlusConfigs);
//        if (localPlusConfig != null) {
//            String storagePath = localPlusConfig.getStoragePath();
//            String[] pathPatterns = localPlusConfig.getPathPatterns();
//            if (StrUtil.isNotBlank(storagePath) && ArrayUtil.isNotEmpty(pathPatterns)) {
//                //windows配置./file/，访问会失败，这里替换为绝对路径
//                String os = OshiUtil.getOs().getFamily().toLowerCase();
//                if (StrUtil.startWithIgnoreCase(os, OSConstant.WIN)) {
//                    if (StrUtil.startWithIgnoreCase(storagePath, FileConstant.FILE_PATH_RELATIVE)) {
//                        String absolutePath = FileUtil.getAbsolutePath(StrUtil.EMPTY);
//                        if (StrUtil.isNotBlank(absolutePath)) {
//                            storagePath = StrUtil.replace(storagePath, FileConstant.FILE_PATH_RELATIVE, absolutePath);
//                        }
//                    }
//                }
//                String pathUtl = FileUtil.PATH_FILE_PRE + StrUtil.replace(storagePath, "\\", "/");
//                String pathPattern = ArrayUtil.get(pathPatterns, 0);
//                registry.addResourceHandler(pathPattern)
//                        .addResourceLocations(pathUtl).setCachePeriod(0);
//            }
//        }
//    }
//}
